package atempdao;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import dao.EgnPortfolio;

import util.HibernateUtil;

public class EgnPortfolioDao {
	private final static Logger logger = LoggerFactory.getLogger(EgnPortfolioDao.class);

	public static List<EgnPortfolio> getAllEgnPortfolio() {
		List<EgnPortfolio> rst ;
		Session s = HibernateUtil.currentSession();
		logger.debug("Get all EgnPortfolio instance 00 ");
		Query qr = s.createQuery("from EgnPortfolio" );

		try {
			rst = qr.list();
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
	}

	public static List<EgnPortfolio> getRootEngPortfolio(){
		List<EgnPortfolio> rst ;
		Session s = HibernateUtil.currentSession();
		logger.debug("Get all EgnPortfolio instance 00 ");
		Query qr = s.createQuery(
				" from EgnPortfolio a " +
				" where a.portfolioId not in " +
				" ( select b.id.refPortfolioId from EgnPortfolioDetailSynth b )" 
				);

		try {
			rst = qr.list();
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
		
	}
	public static EgnPortfolio getEgnPortfolio(String portfolioId) {
		EgnPortfolio rst ;
		Session s = HibernateUtil.currentSession();
		logger.debug("Get all EgnPortfolio instance 11");
		
		Query qr = s.createQuery(" from EgnPortfolio a where a.portfolioId =:param"	);
		qr.setParameter("param", portfolioId);
		try {
			rst = (EgnPortfolio)qr.uniqueResult();
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
		 
	}
	
	public static List<EgnPortfolio> getChildEgnPortfolio(String portfolioId) {
		List<EgnPortfolio> rst ;
		Session s = HibernateUtil.currentSession();
		logger.debug("Get all EgnPortfolio instance 11");
		
		Query qr = s.createQuery(
				 " from EgnPortfolio aa" +
				 " where aa.portfolioId in" +
				 " ( select b.id.refPortfolioId" +
				 "	 from EgnPortfolio a, EgnPortfolioDetailSynth b" +
				 "	 where a.portfolioId = b.id.portfolioId" +
				 "	 and a.id.portfolioId =:param )"
				 );
//				" select new dao.EgnPortfolio(b.id.refPortfolioId )" +
//				" from EgnPortfolio a, EgnPortfolioDetailSynth b " +
//				" where a.portfolioId = b.id.portfolioId " +
//				" and a.id.portfolioId =:param" 
//				);
		qr.setParameter("param", portfolioId);
		try {
			rst = qr.list();
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
	}
	public static List<String> getChildEgnPositionId(String portfolioId) {
		List<String> rst ;
		Session s = HibernateUtil.currentSession();
		logger.debug("Get all getChildEgnPositionId instance 11");
		
		Query qr = s.createQuery(
				" select c.id.contractId from EgnPortfolioDetail c " +
				" where c.id.portfolioId =:param" 
				);
		qr.setParameter("param", portfolioId);
		try {
			rst = qr.list();
			HibernateUtil.closeSession();
			return rst;
		} catch (RuntimeException re) {
			logger.error("failed", re);
			HibernateUtil.closeSession();
			throw re;
		}
	}

}
